require(['highcharts', 'reportCommon'], function () {
    var drvId, drvName, titleStr = "", dateType = "02", enerType = "oil",

        totalEnerOil, totalEnerGas, totalEnerEle, totalEnerOilPer, totalEnerGasPer, totalEnerElePer,//总能耗、百公里能耗、能耗排名
        kmEnerOil, kmEnerGas, kmEnerEle, kmEnerOilPer, kmEnerGasPer, kmEnerElePer,
        enerRankOil, enerRankEle, enerRankGas, enerRankOilPer, enerRankElePer, enerRankGasPer,

        tmpDate = [], carOilData = [], lineOilData = [], carEleData = [], lineEleData = [], carGasData = [], lineGasData = [], carMileData = [], lineAvgData = [],//百公里

        energyPctOil = [], energyPctEle = [], energyPctGas = [],//饼图

        trHtml, trHtmlOil, trHtmlEle, trHtmlGas;//排名

    function driverChange(event, treeId, treeNode) {
        if (treeNode.type === 'DRIVER') {
            drvId = treeNode.id;
            drvName = treeNode.name;
            clear();
            $(".tab-panel3 a")[0].click();
            //标题时间
            titleStr = chartTitle(dateType);
            //总能耗、百公里能耗、能耗排名
            getDrvEnerIdxInfo(drvId, dateType);
            //驾驶员百公里能耗趋势     驾驶员里程趋势
            geDrvEnerTrendInfo(drvId, dateType);
            //能耗排名
            getDrvEnerRankInfo(drvId, dateType);
            //能耗转速分析
            getDrvEnergySpdAna(drvId, dateType);
        } else {
            $.messager.alert("提示", "请选择驾驶员进行查看。");
        }
    }

    //左侧树
    commonFun.leftTreeNodeClickObj['drvAnaEnergyInfo'] = driverChange;

    //清空历史记录
    function clear() {
        //总能耗、百公里能耗、能耗排名
        $("p").html("");
        totalEnerOil = "", totalEnerGas = "", totalEnerEle = "", totalEnerOilPer = "", totalEnerGasPer = "", totalEnerElePer = "",
            kmEnerOil = "", kmEnerGas = "", kmEnerEle = "", kmEnerOilPer = "", kmEnerGasPer = "", kmEnerElePer = "",
            enerRankOil = "", enerRankEle = "", enerRankGas = "", enerRankOilPer = "", enerRankElePer = "", enerRankGasPer = "";
    }

    //顶部日期类型
    $(".time-select a").click(function () {
        clear();
        if (drvId) {
            dateType = $(this).attr('val');
            //标题时间
            titleStr = chartTitle(dateType);
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            //总能耗、百公里能耗、能耗排名
            getDrvEnerIdxInfo(drvId, dateType);
            //驾驶员百公里能耗趋势     驾驶员里程趋势
            geDrvEnerTrendInfo(drvId, dateType);
            //能耗排名
            getDrvEnerRankInfo(drvId, dateType);
            //能耗转速分析
            getDrvEnergySpdAna(drvId, dateType);
        }
    });

    //总能耗
    $(".tab-panel3 a").click(function () {
        var trendCarTmp = [], trendLineTmp = [];
        enerType = $(this).attr('val');
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        if (enerType == "oil") {
            if (totalEnerOil != undefined && kmEnerOil != undefined && enerRankOil != undefined) {
                $("#totalEnerTmp").html(totalEnerOil + "<em>L</em>");
                $("#hundKmEnerTmp").html(kmEnerOil + "<em>L</em>");
                $("#enerRank").html(enerRankOil);
                clickPanel(totalEnerOilPer, kmEnerOilPer, enerRankOilPer);
            } else {
                $("#totalEnerTmp").html("");
                $("#hundKmEnerTmp").html("");
                $("#enerRank").html("");
                clickPanel("", "", "");
            }
            //油 曲线图
            trendCarTmp = carOilData;
            trendLineTmp = lineOilData;
            hundKmEnerCharts(tmpDate, '驾驶员百公里油耗趋势', trendCarTmp, '线路平均百公里油耗趋势', trendLineTmp);
            //排名
            writeTrHtml(trHtmlOil);
            //饼图
            spdAna(energyPctOil);
        } else if (enerType == "ele") {
            if (totalEnerEle != undefined && kmEnerEle != undefined && enerRankEle != undefined) {
                $("#totalEnerTmp").html(totalEnerEle + "<em>KW/H</em>");
                $("#hundKmEnerTmp").html(kmEnerEle + "<em>KW/H</em>");
                $("#enerRank").html(enerRankEle);
                clickPanel(totalEnerElePer, kmEnerElePer, enerRankElePer);
            } else {
                $("#totalEnerTmp").html("");
                $("#hundKmEnerTmp").html("");
                $("#enerRank").html("");
                clickPanel("", "", "");
            }
            //电 折线图
            trendCarTmp = carEleData;
            trendLineTmp = lineEleData;
            hundKmEnerCharts(tmpDate, '驾驶员百公里电耗趋势', trendCarTmp, '线路平均百公里电耗趋势', trendLineTmp);
            //排名
            writeTrHtml(trHtmlEle);
            //饼图
            spdAna(energyPctEle);
        } else if (enerType == "gas") {
            if (totalEnerGas != undefined && kmEnerGas != undefined && enerRankGas != undefined) {
                $("#totalEnerTmp").html(totalEnerGas + "<em>L</em>");
                $("#hundKmEnerTmp").html(kmEnerGas + "<em>L</em>");
                $("#enerRank").html(enerRankGas);
                clickPanel(totalEnerGasPer, kmEnerGasPer, enerRankGasPer);
            } else {
                $("#totalEnerTmp").html("");
                $("#hundKmEnerTmp").html("");
                $("#enerRank").html("");
                clickPanel("", "", "");
            }
            //油 曲线图
            trendCarTmp = carGasData;
            trendLineTmp = lineGasData;
            hundKmEnerCharts(tmpDate, '驾驶员百公里气耗趋势', trendCarTmp, '线路平均百公里气耗趋势', trendLineTmp);
            //排名
            writeTrHtml(trHtmlGas);
            //饼图
            spdAna(energyPctGas);
        }
    });

    
    function clickPanel(temp1, temp2, temp3) {
        var t1 = temp1, t2 = temp2, t3 = temp3;
        if (temp1 != undefined && temp1 != "" && temp2 != undefined && temp2 != "" && temp3 != undefined && temp3 != "") {
            if (t1.replace("%", "") > -1) {
                $("#totalEnerOilPer").addClass("icon-arrow-up");
            } else {
                $("#totalEnerOilPer").addClass("icon-arrow-up");
                $("#totalEnerOilPer").addClass("icon-arrow-down");
            }
            if (t2.replace("%", "") > -1) {
                $("#totalEnerHundKmPer").addClass("icon-arrow-up");
            } else {
                $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                $("#totalEnerHundKmPer").addClass("icon-arrow-down");
            }
            if (t3.replace("%", "") > -1) {
                $("#totalEnerRankPer").addClass("icon-arrow-up");
            } else {
                $("#totalEnerRankPer").addClass("icon-arrow-up");
                $("#totalEnerRankPer").addClass("icon-arrow-down");
            }
            $("#totalEnerOilPer").html(temp1);
            $("#totalEnerHundKmPer").html(temp2);
            $("#totalEnerRankPer").html(temp3);
        } else {
            $("#totalEnerOilPer").removeClass("icon-arrow-up");
            $("#totalEnerHundKmPer").removeClass("icon-arrow-up");
            $("#totalEnerRankPer").removeClass("icon-arrow-up");
            $("#totalEnerOilPer").html("");
            $("#totalEnerHundKmPer").html("");
            $("#totalEnerRankPer").html("");
        }
    }

    //总能耗、百公里能耗、能耗排名
    function getDrvEnerIdxInfo(drvId, dateType) {
        $("#totalEnerOilPer").removeClass();
        $("#totalEnerHundKmPer").removeClass();
        $("#totalEnerRankPer").removeClass();
        $.ajaxExt({
            url: "/drvAna/getDrvEnerIdxInfo.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            $("p").html("");
            clickPanel("", "", "");
            $(data).each(function (i, item) {
                if (item.idxType == "060401") {
                    totalEnerOil = item.idxValue;
                    totalEnerOilPer = item.idxMomPer;
                    if (totalEnerOilPer.replace("%", "") > -1) {
                        $("#totalEnerOilPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerOilPer").addClass("icon-arrow-up");
                        $("#totalEnerOilPer").addClass("icon-arrow-down");
                    }
                    $("#totalEnerTmp").html(totalEnerOil + "L");
                    $("#totalEnerOilPer").html(totalEnerOilPer);
                } else if (item.idxType == "060402") {
                    totalEnerEle = item.idxValue;
                    totalEnerElePer = item.idxMomPer;
                    if (totalEnerElePer.replace("%", "") > -1) {
                        $("#totalEnerElePer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerElePer").addClass("icon-arrow-up");
                        $("#totalEnerElePer").addClass("icon-arrow-down");
                    }
                    $("#totalEnerTmp").html(totalEnerEle + "KWH");
                    $("#totalEnerElePer").html(totalEnerElePer);
                } else if (item.idxType == "060403") {
                    totalEnerGas = item.idxValue;
                    totalEnerGasPer = item.idxMomPer;
                    if (totalEnerGasPer.replace("%", "") > -1) {
                        $("#totalEnerGasPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerGasPer").addClass("icon-arrow-up");
                        $("#totalEnerGasPer").addClass("icon-arrow-down");
                    }
                    $("#totalEnerTmp").html(totalEnerGas + "L");
                    $("#totalEnerGasPer").html(totalEnerGasPer);
                } else if (item.idxType == "060404") {
                    kmEnerOil = item.idxValue;
                    kmEnerOilPer = item.idxMomPer;
                    if (kmEnerOilPer.replace("%", "") > -1) {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                        $("#totalEnerHundKmPer").addClass("icon-arrow-down");
                    }
                    $("#hundKmEnerTmp").html(kmEnerOil + "L");
                    $("#totalEnerHundKmPer").html(kmEnerOilPer);
                } else if (item.idxType == "060405") {
                    kmEnerEle = item.idxValue;
                    kmEnerElePer = item.idxMomPer;
                    if (kmEnerElePer.replace("%", "") > -1) {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                        $("#totalEnerHundKmPer").addClass("icon-arrow-down");
                    }
                    $("#hundKmEnerTmp").html(kmEnerEle + "KWH");
                    $("#totalEnerHundKmPer").html(kmEnerElePer);
                } else if (item.idxType == "060406") {
                    kmEnerGas = item.idxValue;
                    kmEnerGasPer = item.idxMomPer;
                    if (kmEnerGasPer.replace("%", "") > -1) {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerHundKmPer").addClass("icon-arrow-up");
                        $("#totalEnerHundKmPer").addClass("icon-arrow-down");
                    }
                    $("#hundKmEnerTmp").html(kmEnerGas + "L");
                    $("#totalEnerHundKmPer").html(kmEnerGasPer);
                } else if (item.idxType == "060407") {
                    enerRankOil = item.idxValue;
                    enerRankOilPer = item.idxMomPer;
                    if (enerRankOilPer.replace("%", "") > -1) {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                        $("#totalEnerRankPer").addClass("icon-arrow-down");
                    }
                    $("#enerRank").html(item.idxValue);
                    $("#totalEnerRankPer").html(enerRankOilPer);
                } else if (item.idxType == "060408") {
                    enerRankEle = item.idxValue;
                    enerRankElePer = item.idxMomPer;
                    if (enerRankElePer.replace("%", "") > -1) {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                        $("#totalEnerRankPer").addClass("icon-arrow-down");
                    }
                    $("#enerRank").html(item.idxValue);
                    $("#totalEnerRankPer").html(enerRankElePer);
                } else if (item.idxType == "060409") {
                    enerRankGas = item.idxValue;
                    enerRankGasPer = item.idxMomPer;
                    if (enerRankGasPer.replace("%", "") > -1) {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                    } else {
                        $("#totalEnerRankPer").addClass("icon-arrow-up");
                        $("#totalEnerRankPer").addClass("icon-arrow-down");
                    }
                    $("#enerRank").html(item.idxValue);
                    $("#totalEnerRankPer").html(enerRankGasPer);
                }
            });
        });
    }

    //驾驶员百公里能耗趋势     驾驶员里程趋势
    function geDrvEnerTrendInfo(drvId, dateType) {
        tmpDate = [], carOilData = [], lineOilData = [], carEleData = [], lineEleData = [], carGasData = [], lineGasData = [], carMileData = [], lineAvgData = [];
        $.ajaxExt({
            url: "/drvAna/geDrvEnerTrendInfo.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            $(data).each(function (i, item) {
                xAxisDateFormat(dateType, tmpDate, item.REPORT_DATE);
//                if (item.IDX_TYPE == "060501") {
                    if (item.TREND_VAL1 == "驾驶员百公里油耗趋势") {
                        carOilData.push(parseFloat(item.TREND_VAL2) || 0);
                    } else if (item.TREND_VAL1 == "驾驶员百公里电耗趋势") {
                        carEleData.push(parseFloat(item.TREND_VAL2) || 0);
                    } else if (item.TREND_VAL1 == "驾驶员百公里气耗趋势") {
                        carGasData.push(parseFloat(item.TREND_VAL2) || 0);
                    }
//                } else if (item.IDX_TYPE == "060502") {
                    else if (item.TREND_VAL1 == "线路平均百公里油耗趋势") {
                        lineOilData.push(parseFloat(item.TREND_VAL2) || 0);
                    } else if (item.TREND_VAL1 == "线路平均百公里气耗趋势") {
                        lineGasData.push(parseFloat(item.TREND_VAL2) || 0);
                    } else if (item.TREND_VAL1 == "线路平均百公里电耗趋势") {
                        lineEleData.push(parseFloat(item.TREND_VAL2) || 0);
                    }
//                } else if (item.IDX_TYPE == "060601") {
                    else if (item.TREND_VAL1 == "线路平均里程趋势") {
                        lineAvgData.push(parseFloat(item.TREND_VAL2) || 0);
                    } else if (item.TREND_VAL1 == "驾驶员里程趋势") {
                        carMileData.push(parseFloat(item.TREND_VAL2) || 0);
                    }
//                }
            });
            //曲线图
            if(enerType == "oil"){
            	hundKmEnerCharts($.unique(tmpDate), '驾驶员百公里油耗趋势', carOilData, '线路平均百公里油耗趋势', lineOilData);
            }else if(enerType == "ele"){
            	hundKmEnerCharts($.unique(tmpDate), '驾驶员百公里油耗趋势', carEleData, '线路平均百公里油耗趋势', lineEleData);
            }else if(enerType == "gas"){
            	hundKmEnerCharts($.unique(tmpDate), '驾驶员百公里油耗趋势', carGasData, '线路平均百公里油耗趋势', lineGasData);
            }
            
            //柱状图
            $('#carMileTrend').highcharts({
                chart: {
                    type: 'column'
                },
                title: {
                    text: "驾驶员：" + drvName + ' ' + titleStr,
                    style: {
                        fontFamily: 'sans-serif',
                        fontSize: '14px'
                    }
                },
                subtitle: {
                    text: ''
                },
                credits: {
                    enabled: false
                },
                xAxis: {
                    categories: $.unique(tmpDate)
                },
                yAxis: {
                    title: {
                        text: 'L'
                    }
                },
                tooltip: {
                    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f} Km</b></td></tr>',
                    footerFormat: '</table>',
                    shared: true,
                    useHTML: true
                },
                plotOptions: {
                    column: {
                        pointPadding: 0.2,
                        borderWidth: 0
                    }
                },
                series: [{
                    name: '驾驶员里程趋势',
                    data: carMileData

                }, {
                    name: '线路平均里程趋势',
                    data: lineAvgData

                }]
            });
        });
    }

    function getDrvEnerRankInfo(drvId, dateType) {
        $.ajaxExt({
            url: "/drvAna/getDrvEnerRankInfo.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            //排名
            trHtmlOil = "", trHtmlEle = "", trHtmlGas = "";
            $(data).each(function (i, item) {
                if (item.IDX_TYPE == "060701") {
                    //能耗排名  油
                    trHtmlOil += "<tr><td><span class='rank-tip'>" + item.RANK_SN + "</span></td><td>" + item.DRV_NAME + "</td><td>" + item.RANK_VAL1 + "L</td></tr>";
                } else if (item.IDX_TYPE == "060702") {
                    //能耗排名   电
                    trHtmlEle += "<tr><td><span class='rank-tip'>" + item.RANK_SN + "</span></td><td>" + item.DRV_NAME + "</td><td>" + item.RANK_VAL1 + "KW/H</td></tr>";
                } else if (item.IDX_TYPE == "060703") {
                    //能耗排名 气
                    trHtmlGas += "<tr><td><span class='rank-tip'>" + item.RANK_SN + "</span></td><td>" + item.DRV_NAME + "</td><td>" + item.RANK_VAL1 + "L</td></tr>";
                }
            });
            if(enerType == "oil"){
            	writeTrHtml(trHtmlOil);
            }else if(enerType == "ele"){
            	writeTrHtml(trHtmlEle);
            }else if(enerType == "gas"){
            	writeTrHtml(trHtmlGas);
            }
        });
    }

    //饼图
    function getDrvEnergySpdAna(drvId, dateType) {
        //饼图
        energyPctOil = [], energyPctEle = [], energyPctGas = [];
        $.ajaxExt({
            url: "/drvAna/getDrvEnergySpdAna.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            $(data).each(function (i, item) {
                if (item.IDX_TYPE == "060801") {
                    //饼图 填充数据  油
                    energyPctOil.push([item.RANK_VAL1, parseInt(item.RANK_VAL2) || 0]);
                } else if (item.IDX_TYPE == "060802") {
                    //饼图 填充数据  电
                    energyPctEle.push([item.RANK_VAL1, parseInt(item.RANK_VAL2) || 0]);
                } else if (item.IDX_TYPE == "060803") {
                    //饼图 填充数据  气
                    energyPctGas.push([item.RANK_VAL1, parseInt(item.RANK_VAL2) || 0]);
                }
            });
            if(enerType == "oil"){
            	spdAna(energyPctOil);
            }else if(enerType == "ele"){
            	spdAna(energyPctEle);
            }else if(enerType == "gas"){
            	spdAna(energyPctGas);
            }
//            spdAna(energyPctOil);
        });
    }

    //曲线图
    function hundKmEnerCharts(tmpDate, trendCarName, trendCarTmp, trendLineName, trendLineTmp) {
        $('#carHundKmEnerTrend').highcharts({
            chart: {
                type: 'spline'
            },
            title: {
                text: "驾驶员：" + drvName + ' ' + titleStr,
                style: {
                    fontFamily: 'sans-serif',
                    fontSize: '14px'
                }
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: tmpDate
            },
            yAxis: {
                title: {
                    text: 'KM'
                }
            },
            credits: {
                enabled: false
            },
            tooltip: {
                crosshairs: true,
                shared: true
            },
            plotOptions: {
                line: {
                    dataLabels: {
                        enabled: true
                    },
                    enableMouseTracking: false
                }
            },
            series: [{
                name: trendCarName,
                data: trendCarTmp
            }, {
                name: trendLineName,
                data: trendLineTmp
            }]
        });
    }

    //转速分析
    function spdAna(energyPct) {
        $("#speedAnalysis").highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: "驾驶员：" + drvName + ' ' + titleStr,
                style: {
                    fontFamily: 'sans-serif',
                    fontSize: '14px'
                }
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                    }
                }
            },
            credits: {
                enabled: false
            },
            series: [
                {
                    type: 'pie',
                    name: '转速分析',
                    data: energyPct
                }
            ]
        });
    }

    //排名
    function writeTrHtml(trHtmlFlag) {
        trHtml = "<tr><th width='15%'>排名</th><th width='40%'>驾驶员</th><th>百公里能耗</th></tr>";
        trHtml += trHtmlFlag;
        $("#trHtml").html(trHtml);
    }

    //如果左侧树有选择驾驶员则直接查询该驾驶员数据
    business.execFnByLeftTreeSelectNode('DRIVER', driverChange);
});